package afcp.alumni.action.base;

import java.util.Locale;

import net.sourceforge.stripes.validation.ValidationErrors;
import afcp.alumni.model.SearchResult;
import afcp.alumni.model.URL;
import afcp.alumni.util.Conf;

@Deprecated
public class Paginator<T> {
	
	public Paginator(String pageParam) {
		this.pageParam = pageParam;
	}
	
	private int page = 1;
	private int perPage = Integer.parseInt(Conf.get("pagination.perpage"));
	private SearchResult<T> results;
	private URL url;
	private String pageParam;
	
	public int getpMax() {
		return results != null ? results.getpMax() : -1;
	}
	public int getNextPage() {
		return results.getNextPage();
	}
	public int getPreviousPage() {
		return results.getPreviousPage();
	}
	public boolean getHasNextPage() {
		return results != null ? results.getHasNextPage() : false;
	}
	public boolean getHasPreviousPage() {
		return results != null ? results.getHasPreviousPage() : false;
	}
	public String getNextPageUrl() {
		url.addParam(pageParam, String.valueOf(getNextPage()));
		return url.getFullUrl();
	}
	public String getPreviousPageUrl() {
		url.addParam(pageParam, String.valueOf(getPreviousPage()));
		return url.getFullUrl();
	}


	public int getPage() {
//		return results != null ? results.getPage() : page;
		return page;
	}
	public int getPerPage() {
		return perPage;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public void setPerPage(int perPage) {
		this.perPage = perPage;
	}
	public SearchResult<T> getResults() {
		return results;
	}
	public void setResults(SearchResult<T> results, URL currentUrl) {
		this.results = results;
		url = currentUrl;
	}

	
	public void checkPageValue(ValidationErrors errors) {
		// query is run before custom validation
		// check required page number
		int pMax = results.getpMax();
		if (page < 1) {
			BaseAction.addError(errors, "validation.minvalue.valueBelowMinimum2", BaseAction.msg(Locale.getDefault(), "p"), 1);
		} else if (page > pMax) {
			BaseAction.addError(errors, "validation.maxvalue.valueAboveMaximum2", BaseAction.msg(Locale.getDefault(), "p"), pMax);
		}
	}

}
